# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'notewidget.ui'
#
# Created: Mon May 14 20:00:27 2012
#      by: PyQt4 UI code generator 4.9.1
#
# WARNING! All changes made in this file will be lost!

from PyQt4 import QtCore, QtGui
from pl.edu.budgetanalizer.GUI.notesManagment import EditNote
from pl.edu.budgetanalizer.model.Product import Product
import pyodbc

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    _fromUtf8 = lambda s: s

class Ui_NoteWidget(QtGui.QWidget):
    
    listOfProducts = []
    lastProductY = 60
    amount = 0
    
    def weryf(self):
        conn = pyodbc.connect(self.databaseDir)
        cur = conn.cursor()
        cur.execute("exec deleteOldNote ?",self.id_note)
        conn.commit()
        for p in self.listOfProducts:
            cur.execute("exec getProductId ?, ?",p.name,p.producer)
            row = cur.fetchone()
            product_id = row[0]
            cur.execute("exec addOldNoteProduct ?, ?, ?, ?",self.id_note,product_id,p.quantity,p.price)
            conn.commit()
        conn.close()
        
    def getAllProductsOnNote(self):
        '''
        Pobiera wszystkie produkty dla danej notatki
        '''
        self.listOfProducts = []
        conn = pyodbc.connect(self.databaseDir)
        cur = conn.cursor()
        cur.execute("exec getAllProductsForNote ?",self.id_note)
        rows = cur.fetchall()
        for row in rows:
            product = Product(row[0],row[1],row[2],row[3],row[4],row[5])
            print row[0]
            self.listOfProducts.append(product)
            self.amount += row[4]
        conn.close()
        
    def getTopic(self):
        conn = pyodbc.connect(self.databaseDir)
        cur = conn.cursor()
        cur.execute("exec getTopic ?",self.id_note)
        row = cur.fetchone()
        self.topic = row[0]
        conn.close()
        
    
    def editNote(self):
        '''
        Naklada nowego widgeta na centrum aplikacji
        '''
#        conn = pyodbc.connect(self.databaseDir)
#        cur = conn.cursor()
#        day = self.date.day()
#        month = self.date.month()
#        year = self.date.year()
#        date = str(year)+"-"+str(month)+"-"+str(day)
#        cur.execute("exec getNoteId ?, ?, ?",date,str(self.topic),"user")
#        row = cur.fetchone()
#        id_note = row[0]
#        conn.close()
        self.newNoteWidget = EditNote.Ui_EditNote(self)
        self.newNoteWidget.setupUi(self.newNoteWidget,self.date,[],self.databaseDir,self.topic,self.listOfProducts,self.id_note,[])
        self.newNoteWidget.setGeometry(QtCore.QRect(0, 0, 461, 511))
        self.newNoteWidget.setAutoFillBackground(True)
        self.newNoteWidget.show()

#    def setAmount(self):
#        conn = pyodbc.connect(self.databaseDir)
#        cur = conn.cursor()
#        cur.execute("select getSum(?)",self.id_note)
#        rows = cur.fetchone()
#        self.amount = rows[0]

        
    def addProduct(self,product):
        '''
        Odswieza widok produktow
        '''
        horizontalLayoutWidget_6 = QtGui.QWidget(self.noteScrollArea)
        horizontalLayoutWidget_6.setGeometry(QtCore.QRect(10, self.lastProductY, 411, 31))
        self.lastProductY += 20
        horizontalLayoutWidget_6.setObjectName(_fromUtf8("horizontalLayoutWidget_6"))
        productHorizontalLayout = QtGui.QHBoxLayout(horizontalLayoutWidget_6)
        productHorizontalLayout.setMargin(0)
        
        #name
        productNameLabel = QtGui.QLabel(horizontalLayoutWidget_6)
        sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Preferred)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(productNameLabel.sizePolicy().hasHeightForWidth())
        productNameLabel.setSizePolicy(sizePolicy)
        productNameLabel.setMinimumSize(QtCore.QSize(130, 0))
        productNameLabel.setObjectName(_fromUtf8("productNameLabel"))
        productHorizontalLayout.addWidget(productNameLabel)
        
        #quantity
        quantityNumberLabel = QtGui.QLabel(horizontalLayoutWidget_6)
        sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Preferred)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(quantityNumberLabel.sizePolicy().hasHeightForWidth())
        quantityNumberLabel.setSizePolicy(sizePolicy)
        quantityNumberLabel.setMinimumSize(QtCore.QSize(100, 0))
        quantityNumberLabel.setObjectName(_fromUtf8("quantityNumberLabel"))
        productHorizontalLayout.addWidget(quantityNumberLabel)
        
        quantityMeasureLabel = QtGui.QLabel(horizontalLayoutWidget_6)
        quantityMeasureLabel.setObjectName(_fromUtf8("quantityMeasureLabel"))
        productHorizontalLayout.addWidget(quantityMeasureLabel)
        
        #price
        priceNumberLabel = QtGui.QLabel(horizontalLayoutWidget_6)
        priceNumberLabel.setMinimumSize(QtCore.QSize(100, 0))
        priceNumberLabel.setObjectName(_fromUtf8("priceNumberLabel"))
        productHorizontalLayout.addWidget(priceNumberLabel)
        
        priceMeasureLabel = QtGui.QLabel(horizontalLayoutWidget_6)
        priceMeasureLabel.setObjectName(_fromUtf8("priceMeasureLabel"))
        productHorizontalLayout.addWidget(priceMeasureLabel)
        
        productNameLabel.setText(QtGui.QApplication.translate("NoteWidget", product.name + ", " + product.producer, None, QtGui.QApplication.UnicodeUTF8))
        quantityNumberLabel.setText(QtGui.QApplication.translate("NoteWidget", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
"p, li { white-space: pre-wrap; }\n"
"</style></head><body style=\" font-family:\'MS Shell Dlg 2\'; font-size:8.25pt; font-weight:400; font-style:normal;\">\n"
"<p align=\"right\" style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:8pt;\">"+QtCore.QString().setNum(product.quantity)+ "</span></p></body></html>", None, QtGui.QApplication.UnicodeUTF8))
        quantityMeasureLabel.setText(QtGui.QApplication.translate("NoteWidget", product.measure, None, QtGui.QApplication.UnicodeUTF8))
        priceNumberLabel.setText(QtGui.QApplication.translate("NoteWidget", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
"p, li { white-space: pre-wrap; }\n"
"</style></head><body style=\" font-family:\'MS Shell Dlg 2\'; font-size:8.25pt; font-weight:400; font-style:normal;\">\n"
"<p align=\"right\" style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:8pt;\">"+QtCore.QString().setNum(product.price)+"</span></p></body></html>", None, QtGui.QApplication.UnicodeUTF8))
        priceMeasureLabel.setText(QtGui.QApplication.translate("NoteWidget", "zl", None, QtGui.QApplication.UnicodeUTF8))
        
    
    '''
    wygenerowane gui
    '''
    def setupUi(self, NoteWidget,databaseDir,id_note,date):
        
        self.date = date
        self.id_note = id_note
        self.databaseDir = databaseDir
        NoteWidget.setObjectName(_fromUtf8("NoteWidget"))
        NoteWidget.resize(430, 243)
        self.noteScrollArea = QtGui.QWidget(NoteWidget)
        self.noteScrollArea.setGeometry(QtCore.QRect(0, 0, 450,400))
        self.noteScrollArea.setObjectName(_fromUtf8("noteScrollArea"))

        
        #topic
        self.horizontalLayoutWidget_3 = QtGui.QWidget(self.noteScrollArea)
        self.horizontalLayoutWidget_3.setGeometry(QtCore.QRect(10, 10, 411, 21))
        self.horizontalLayoutWidget_3.setObjectName(_fromUtf8("horizontalLayoutWidget_3"))
        self.topicHorizontalLayout = QtGui.QHBoxLayout(self.horizontalLayoutWidget_3)
        self.topicHorizontalLayout.setMargin(0)
        self.topicHorizontalLayout.setObjectName(_fromUtf8("topicHorizontalLayout"))
        self.topicLabel = QtGui.QLabel(self.horizontalLayoutWidget_3)
        self.topicLabel.setObjectName(_fromUtf8("topicLabel"))
        self.topicHorizontalLayout.addWidget(self.topicLabel)
        self.topicTextLabel = QtGui.QLabel(self.horizontalLayoutWidget_3)
        sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Preferred)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.topicTextLabel.sizePolicy().hasHeightForWidth())
        self.topicTextLabel.setSizePolicy(sizePolicy)
        self.topicTextLabel.setMinimumSize(QtCore.QSize(300, 0))
        self.topicTextLabel.setObjectName(_fromUtf8("topicTextLabel"))
        self.topicHorizontalLayout.addWidget(self.topicTextLabel)
        self.getTopic()
        
        #labels
        self.horizontalLayoutWidget_5 = QtGui.QWidget(self.noteScrollArea)
        self.horizontalLayoutWidget_5.setGeometry(QtCore.QRect(10, 40, 411, 31))
        self.horizontalLayoutWidget_5.setObjectName(_fromUtf8("horizontalLayoutWidget_5"))
        self.labelsHorizontalLayout = QtGui.QHBoxLayout(self.horizontalLayoutWidget_5)
        self.labelsHorizontalLayout.setMargin(0)
        self.labelsHorizontalLayout.setObjectName(_fromUtf8("labelsHorizontalLayout"))
        self.produtLabel = QtGui.QLabel(self.horizontalLayoutWidget_5)
        self.produtLabel.setObjectName(_fromUtf8("produtLabel"))
        self.labelsHorizontalLayout.addWidget(self.produtLabel)
        self.quantityLabel = QtGui.QLabel(self.horizontalLayoutWidget_5)
        self.quantityLabel.setObjectName(_fromUtf8("quantityLabel"))
        self.labelsHorizontalLayout.addWidget(self.quantityLabel)
        self.priceLabel = QtGui.QLabel(self.horizontalLayoutWidget_5)
        self.priceLabel.setObjectName(_fromUtf8("priceLabel"))
        self.labelsHorizontalLayout.addWidget(self.priceLabel)
        
        if self.id_note <> 0:
            self.getAllProductsOnNote()
            for p in self.listOfProducts:
                self.addProduct(p)
    
        #amount
        self.horizontalLayoutWidget_2 = QtGui.QWidget(self.noteScrollArea)
        self.horizontalLayoutWidget_2.setGeometry(QtCore.QRect(300, self.lastProductY+30, 91, 21))
        self.horizontalLayoutWidget_2.setObjectName(_fromUtf8("horizontalLayoutWidget_2"))
        self.amoutHorizontalLayout = QtGui.QHBoxLayout(self.horizontalLayoutWidget_2)
        self.amoutHorizontalLayout.setMargin(0)
        self.amoutHorizontalLayout.setObjectName(_fromUtf8("amoutHorizontalLayout"))
        self.amoutLabel = QtGui.QLabel(self.horizontalLayoutWidget_2)
        self.amoutLabel.setObjectName(_fromUtf8("amoutLabel"))
        self.amoutHorizontalLayout.addWidget(self.amoutLabel)
        self.amoutNumberLabel = QtGui.QLabel(self.horizontalLayoutWidget_2)
        self.amoutNumberLabel.setObjectName(_fromUtf8("amoutNumberLabel"))
        self.amoutHorizontalLayout.addWidget(self.amoutNumberLabel)
        self.amoutMeaureLabel = QtGui.QLabel(self.horizontalLayoutWidget_2)
        sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Preferred)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.amoutMeaureLabel.sizePolicy().hasHeightForWidth())
        self.amoutMeaureLabel.setSizePolicy(sizePolicy)
        self.amoutMeaureLabel.setMinimumSize(QtCore.QSize(2, 0))
        self.amoutMeaureLabel.setObjectName(_fromUtf8("amoutMeaureLabel"))
        self.amoutHorizontalLayout.addWidget(self.amoutMeaureLabel)
        
        #buttons
        self.editButton = QtGui.QPushButton(self.noteScrollArea)
        self.editButton.setObjectName(_fromUtf8("editButton"))
        self.editButton.connect(self.editButton, QtCore.SIGNAL("clicked()"), self.editNote)
        self.editButton.setGeometry(QtCore.QRect(220, 370, 100, 31))
        
        self.weryfButton = QtGui.QPushButton(self.noteScrollArea)
        self.weryfButton.setObjectName(_fromUtf8("weryfButton"))
        self.weryfButton.connect(self.weryfButton, QtCore.SIGNAL("clicked()"), self.weryf)
        self.weryfButton.setGeometry(QtCore.QRect(320, 370, 100, 31))
        

        self.retranslateUi(NoteWidget)
        QtCore.QMetaObject.connectSlotsByName(NoteWidget)

    def retranslateUi(self, NoteWidget):
        NoteWidget.setWindowTitle(QtGui.QApplication.translate("NoteWidget", "Form", None, QtGui.QApplication.UnicodeUTF8))
        self.topicLabel.setText(QtGui.QApplication.translate("NoteWidget", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
"p, li { white-space: pre-wrap; }\n"
"</style></head><body style=\" font-family:\'MS Shell Dlg 2\'; font-size:8.25pt; font-weight:400; font-style:normal;\">\n"
"<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:11pt;\">Temat: </span></p></body></html>", None, QtGui.QApplication.UnicodeUTF8))
        self.topicTextLabel.setText(QtGui.QApplication.translate("NoteWidget", self.topic, None, QtGui.QApplication.UnicodeUTF8))
        self.produtLabel.setText(QtGui.QApplication.translate("NoteWidget", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
"p, li { white-space: pre-wrap; }\n"
"</style></head><body style=\" font-family:\'MS Shell Dlg 2\'; font-size:8.25pt; font-weight:400; font-style:normal;\">\n"
"<p align=\"center\" style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:8pt;\">Produkt</span></p></body></html>", None, QtGui.QApplication.UnicodeUTF8))
        self.quantityLabel.setText(QtGui.QApplication.translate("NoteWidget", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
"p, li { white-space: pre-wrap; }\n"
"</style></head><body style=\" font-family:\'MS Shell Dlg 2\'; font-size:8.25pt; font-weight:400; font-style:normal;\">\n"
"<p align=\"center\" style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:8pt;\">Ilość</span></p></body></html>", None, QtGui.QApplication.UnicodeUTF8))
        self.priceLabel.setText(QtGui.QApplication.translate("NoteWidget", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
"p, li { white-space: pre-wrap; }\n"
"</style></head><body style=\" font-family:\'MS Shell Dlg 2\'; font-size:8.25pt; font-weight:400; font-style:normal;\">\n"
"<p align=\"center\" style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:8pt;\">Cena</span></p></body></html>", None, QtGui.QApplication.UnicodeUTF8))
        self.amoutNumberLabel.setText(QtGui.QApplication.translate("NoteWidget", str(self.amount), None, QtGui.QApplication.UnicodeUTF8))
        self.amoutMeaureLabel.setText(QtGui.QApplication.translate("NoteWidget", "zl", None, QtGui.QApplication.UnicodeUTF8))
        self.amoutLabel.setText(QtGui.QApplication.translate("NoteWidget", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
"p, li { white-space: pre-wrap; }\n"
"</style></head><body style=\" font-family:\'MS Shell Dlg 2\'; font-size:8.25pt; font-weight:400; font-style:normal;\">\n"
"<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:11pt;\">Suma:</span></p></body></html>", None, QtGui.QApplication.UnicodeUTF8))
        self.editButton.setText(QtGui.QApplication.translate("dayWidget", "Edytuj", None, QtGui.QApplication.UnicodeUTF8))
        self.weryfButton.setText(QtGui.QApplication.translate("dayWidget", "Weryfikuj", None, QtGui.QApplication.UnicodeUTF8))
